#ifndef TRACEABLE_EXCEPTION_H
#define TRACEABLE_EXCEPTION_H

#include <execinfo.h>
#include <unistd.h>
#include <sys/types.h>

#include <string>
#include <vector>

class TraceableException{
	public:
		TraceableException()throw(){
			int size=0;
			const int block_size=10;
			std::vector<void*> stack_trace;
			void** array;
			char **strings;
			do{
				void* array[block_size];
				size = backtrace (array, block_size);
				std::copy(array, array+block_size, stack_trace.end());
				
			}while(size==block_size);
			array=new void*[stack_trace.size()];
			std::copy(stack_trace.begin(), stack_trace.end(), array);
			strings=backtrace_symbols (array, stack_trace.size());
			std::copy(strings, strings+stack_trace.size(), this->stack_trace.end());
			free(strings);
			delete[] array;
		}
		virtual ~TraceableException()throw(){
		
		}
		const std::vector<std::string>& trace()const{
			return stack_trace;
		}
	private:
		std::vector<std::string> stack_trace;
};

#endif
